
// Assigment01_DoTranVanDlg.cpp : implementation file
//

#include "stdafx.h"
#include "StaffManager.h"
#include "StaffManagerDlg.h"
#include "AddInfo.h"
#include "StaffItem.h"
//#include "iostream.h"
using namespace std;

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	enum { IDD = IDD_ABOUTBOX };

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

// Implementation
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()


// StaffManagerDlg dialog




StaffManagerDlg::StaffManagerDlg(CWnd* pParent /*=NULL*/)
: CDialog(StaffManagerDlg::IDD, pParent)
	
	, m_result(_T(""))
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void StaffManagerDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_LIST_INFOMATION, m_cListControl);
	DDX_Text(pDX, IDC_EDT_SHOW, m_result);
}

BEGIN_MESSAGE_MAP(StaffManagerDlg, CDialog)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_BN_CLICKED(IDC_BTN_ADD, &StaffManagerDlg::OnBnClickedBtnAdd)
	ON_NOTIFY(NM_DBLCLK, IDC_LIST_INFOMATION, &StaffManagerDlg::OnNMDblclkListInfomation)
	ON_BN_CLICKED(IDC_BTN_SEARCH, &StaffManagerDlg::OnBnClickedBtnSearch)
END_MESSAGE_MAP()

// StaffManagerDlg message handlers

BOOL StaffManagerDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		BOOL bNameValid;
		CString strAboutMenu;
		bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
		ASSERT(bNameValid);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	m_cListControl.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES | LVS_REPORT);

	// TODO: Add extra initialization here

	m_cListControl.SetHeadings( _T("ID,100;Full Name,170;Age,80;Salary,130;Part,190") );
	m_cListControl.LoadColumnInfo();
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void StaffManagerDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void StaffManagerDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR StaffManagerDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}

void StaffManagerDlg::OnBnClickedBtnAdd()
{
	// TODO: Add your control notification handler code here
	AddInfo m_info;
	if(m_info.DoModal() == 10)
	{
		(void)m_cListControl.AddItem(_T(m_info.m_ID),_T(m_info.m_Name),_T(m_info.m_Age),_T(m_info.m_Salary),_T(m_info.m_Part));
	
	//Store Array
	CStaffItem si(m_info.m_ID, m_info.m_Name, m_info.m_Age, m_info.m_Salary, m_info.m_Part);
	StaffItems.Add(si);
	}
}

void StaffManagerDlg::OnNMDblclkListInfomation(NMHDR *pNMHDR, LRESULT *pResult)
{   
	 AddInfo mInfo;
	 int index = 0;
	 POSITION pos = m_cListControl.GetFirstSelectedItemPosition();
	if(pos != NULL)
	{
		while(pos)
		{    
			int row        = m_cListControl.GetNextSelectedItem(pos);
			mInfo.m_ID     = m_cListControl.GetItemText(row,0);
			mInfo.m_Name   = m_cListControl.GetItemText(row,1);
			mInfo.m_Age    = m_cListControl.GetItemText(row,2);
			mInfo.m_Salary = m_cListControl.GetItemText(row,3);
			mInfo.m_Part   = m_cListControl.GetItemText(row,4);
			index = row;
		}
	}
	if(mInfo.DoModal() == 10)		
	{
		m_cListControl.DeleteItem(index);
		StaffItems[index].SetItemId(mInfo.m_ID);
		StaffItems[index].SetItemName(mInfo.m_Name);
		StaffItems[index].SetItemAge(mInfo.m_Age);
		StaffItems[index].SetItemSalary(mInfo.m_Salary);
		StaffItems[index].SetItemPart(mInfo.m_Part);
		m_cListControl.AddItem(_T(StaffItems[index].GetItemId()),_T(StaffItems[index].GetItemName()),_T(StaffItems[index].GetItemAge()),_T(StaffItems[index].GetItemSalary()),_T(StaffItems[index].GetItemPart()));		
	}
}

void StaffManagerDlg::OnBnClickedBtnSearch()
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	if(m_result=="")
	{
		m_cListControl.DeleteAllItems();
		for(int i=0;i<StaffItems.GetSize();i++)
		{
			m_cListControl.AddItem(_T(StaffItems[i].GetItemId()),_T(StaffItems[i].GetItemName()),_T(StaffItems[i].GetItemAge()),_T(StaffItems[i].GetItemSalary()),_T(StaffItems[i].GetItemPart()));	
		
		}
	}
	else
	{

	for(int i = 0; i < StaffItems.GetSize(); i++)
	{
		if(m_result == StaffItems[i].GetItemId())
		{
			m_cListControl.DeleteAllItems();
			m_cListControl.AddItem(StaffItems[i].GetItemId(), StaffItems[i].GetItemName(), StaffItems[i].GetItemAge(), StaffItems[i].GetItemSalary(), StaffItems[i].GetItemPart());
		}
		
		if(m_result == StaffItems[i].GetItemName())
		{
			m_cListControl.DeleteAllItems();
			m_cListControl.AddItem(StaffItems[i].GetItemId(), StaffItems[i].GetItemName(), StaffItems[i].GetItemAge(), StaffItems[i].GetItemSalary(), StaffItems[i].GetItemPart());
		}
		

		if(m_result == StaffItems[i].GetItemAge())
		{
			m_cListControl.DeleteAllItems();
			m_cListControl.AddItem(StaffItems[i].GetItemId(), StaffItems[i].GetItemName(), StaffItems[i].GetItemAge(), StaffItems[i].GetItemSalary(), StaffItems[i].GetItemPart());
		}
		
		if(m_result == StaffItems[i].GetItemSalary())
		{	
			m_cListControl.DeleteAllItems();
			m_cListControl.AddItem(StaffItems[i].GetItemId(), StaffItems[i].GetItemName(), StaffItems[i].GetItemAge(), StaffItems[i].GetItemSalary(), StaffItems[i].GetItemPart());
		}

		if(m_result == StaffItems[i].GetItemPart())
		{
			m_cListControl.DeleteAllItems();
			m_cListControl.AddItem(StaffItems[i].GetItemId(), StaffItems[i].GetItemName(), StaffItems[i].GetItemAge(), StaffItems[i].GetItemSalary(), StaffItems[i].GetItemPart());
		}
	}
	}
}
